package com.guoheng.safe.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.IdDTO;
import com.guoheng.safe.sys.dto.basicinfo.ControlDataAddDTO;
import com.guoheng.safe.sys.dto.basicinfo.ControlDataPageDTO;
import com.guoheng.safe.sys.dto.basicinfo.ControlDataUpdateDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.guoheng.safe.sys.model.basicinfo.ControlData;
import com.guoheng.safe.sys.mapper.basicinfo.ControlDataMapper;
import com.guoheng.safe.sys.service.ControlDataService;

import java.util.List;

/**
 * 类功能描述:　ControlDataServiceImpl
 *
 * @author Eternal
 * @date 2019-53-19 15:53
 */
@Service
public class ControlDataServiceImpl implements ControlDataService {

    @Resource
    private ControlDataMapper controlDataMapper;


    @Override
    public Result addControlData(ControlDataAddDTO controlDataAddDTO) {
        ControlData data = controlDataMapper.selectOne(controlDataAddDTO.getDeviceType(), controlDataAddDTO.getControlType());
        if(null != data){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DUPLICATED_DATA);
        }
        ControlData controlData = new ControlData();
        BeanUtils.copyProperties(controlDataAddDTO,controlData);
        int result = controlDataMapper.insert(controlData);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result updateControlData(ControlDataUpdateDTO controlDataUpdateDTO) {
        ControlData controlData = new ControlData();
        BeanUtils.copyProperties(controlDataUpdateDTO,controlData);
        int result = controlDataMapper.updateByPrimaryKeySelective(controlData);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result deleteControlData(IdDTO idDTO) {
        int result = controlDataMapper.deleteByPrimaryKey(idDTO.getId());
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DELETE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result getControlData(IdDTO idDTO) {
        ControlData controlData = controlDataMapper.selectByPrimaryKey(idDTO.getId());
        if (null == controlData){
            return Result.failure(ErrorCodeEnum.SYS_ERR_SEARCH_FAILED);
        }
        return Result.success(controlData);
    }

    @Override
    public Result getControlData(ControlDataPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum() , pageDTO.getPageSize());
        List<ControlData> controlData = controlDataMapper.selectAll();
        PageInfo<ControlData> pageInfo = new PageInfo<>(controlData);
        return Result.success(pageInfo);
    }
}

